package cn.iocoder.yudao.module.blog.controller.admin.postertype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.postertype.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.postertype.PosterTypeDO;
import cn.iocoder.yudao.module.blog.convert.postertype.PosterTypeConvert;
import cn.iocoder.yudao.module.blog.service.postertype.PosterTypeService;

@Tag(name = "管理后台 - 海报类型")
@RestController
@RequestMapping("/blog/poster-type")
@Validated
public class PosterTypeController {

    @Resource
    private PosterTypeService posterTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建海报类型")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:create')")
    public CommonResult<Long> createPosterType(@Valid @RequestBody PosterTypeCreateReqVO createReqVO) {
        return success(posterTypeService.createPosterType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新海报类型")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:update')")
    public CommonResult<Boolean> updatePosterType(@Valid @RequestBody PosterTypeUpdateReqVO updateReqVO) {
        posterTypeService.updatePosterType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除海报类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:poster-type:delete')")
    public CommonResult<Boolean> deletePosterType(@RequestParam("id") Long id) {
        posterTypeService.deletePosterType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得海报类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:query')")
    public CommonResult<PosterTypeRespVO> getPosterType(@RequestParam("id") Long id) {
        PosterTypeDO posterType = posterTypeService.getPosterType(id);
        return success(PosterTypeConvert.INSTANCE.convert(posterType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得海报类型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:query')")
    public CommonResult<List<PosterTypeRespVO>> getPosterTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<PosterTypeDO> list = posterTypeService.getPosterTypeList(ids);
        return success(PosterTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得海报类型分页")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:query')")
    public CommonResult<PageResult<PosterTypeRespVO>> getPosterTypePage(@Valid PosterTypePageReqVO pageVO) {
        PageResult<PosterTypeDO> pageResult = posterTypeService.getPosterTypePage(pageVO);
        return success(PosterTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有海报类型")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:query')")
    public CommonResult<List<PosterTypeRespVO>> getAllPoster(){
        List<PosterTypeDO> list = posterTypeService.getAll();

        return success(PosterTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出海报类型 Excel")
    @PreAuthorize("@ss.hasPermission('blog:poster-type:export')")
    @OperateLog(type = EXPORT)
    public void exportPosterTypeExcel(@Valid PosterTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PosterTypeDO> list = posterTypeService.getPosterTypeList(exportReqVO);
        // 导出 Excel
        List<PosterTypeExcelVO> datas = PosterTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "海报类型.xls", "数据", PosterTypeExcelVO.class, datas);
    }

}
